<template>
  <div class="bottom-bar">
    <!-- 全选按钮 -->
    <div class="check-content">
      <checkbutton 
      class="check-button" 
      :is-checked="isSelectAll" 
      @click.native="totalClick"/>
      <span>全选</span>
    </div>
    <!-- 总金额 -->
    <div class="price">合计：{{ totalPrice }}</div>

    <!-- 计算 -->
    <div class="caculate" @click="calcClick">去计算：{{ checkLength }}</div>
  </div>
</template>

<script>
import Checkbutton from "components/content/checkButton/CheckButton.vue";

export default {
  name: "CartButtomBar",
  components: {
    Checkbutton,
  },
  computed: {
    totalPrice() {
      return (
        "￥" +
        this.$store.state.cartList
          .filter((item) => {
            return item.checked; //根据checkde真假，返回item
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },
    //判断是否全选
    isSelectAll(){
      if(this.$store.state.cartList.length==0) return false
      return !(this.$store.state.cartList.filter(item=>!item.checked).length)
    }
  },
  methods: {
    totalClick(){
     if(this.isSelectAll){//全部选中
       this.$store.state.cartList.forEach(item => item.checked=false);
     }else {
      this.$store.state.cartList.forEach(item => item.checked=true);
     }
    },
    calcClick(){
      if(!this.isSelectAll){
        this.$toast.show('请选择购买的商品',2000)
      }
    }
  },
};
</script>

<style scoped>
.bottom-bar {
  position: relative;
  display: flex;
  height: 40px;
  line-height: 40px;
  background-color: #eee;
  font-size: 14px;
}
.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-right: 10px;
}
.price {
  flex: 1;
  margin-left: 20px;
}
.caculate {
  width: 90px;
  background-color: pink;
  text-align: center;
}
</style>